'''PerspectiveToolBar

A toolbar for switching perspectives
'''

import wx
import wx.lib.agw.aui as aui
import firebrick

# FIXME: this module is incomplete. It doesn't update itself if you
# change perspectives via the menubar. It has other problems as well.
class PerspectiveToolBar(aui.AuiToolBar):

    name = "Perspective Toolbar"
    _id = "rwb.toolbar.perspective"
    
    def __init__(self, parent):
        aui.AuiToolBar.__init__(self, parent, wx.ID_ANY, style=aui.AUI_TB_TEXT)

        self.window = parent
        self.chooser = wx.Choice(self, wx.ID_ANY, size=(200,-1))


        font = self.GetFont()
        dc = wx.WindowDC(self)
        dc.SetFont(font)
        label = "Perspective:"
        width, height = dc.GetTextExtent(label)

        self.AddLabel(wx.ID_ANY, label, width)
        self.AddControl(self.chooser, "")
        self.chooser.Bind(wx.EVT_CHOICE, self.OnCombobox)

        perspectives = wx.GetApp().perspectives
        for p in  perspectives:
            self.chooser.Append(p.name, p)
        self.chooser.SetSelection(0)

        parent.auimgr.AddPane(self, aui.AuiPaneInfo().
                              ToolbarPane().
                              Top().
                              Floatable(False).
                              Gripper(True).
                              Name("rwb.toolbar.perspective").
                              Movable(True).
                              Resizable(False).
                              Show(False)
                              )

        firebrick.app.Subscribe("perspective.new", self.OnNewPerspective)

        # without this, auibar throws an "object has no attribute '_absolute_min_size' 
        # error. 
        self.Realize()

    def OnNewPerspective(self, message):
        # need to update the combobox...
        # FIXME: instead of just appending, I need to sort...
        self.chooser.Append(message.data["perspective"])

    def OnCombobox(self, event):
        perspective = self.chooser.GetClientData(event.GetSelection())
        self.window.LoadPerspective(perspective.id)
